package com.class06.controller;

import com.class06.pojo.Cat;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Enumeration;

@RequestMapping("/param")
@Controller
@ResponseBody
public class ParamController {

    //    http://localhost:8866/param/testParam?name=zhangsan&love=lisi
//    一、param接受参数
//   1、param传参的语法格式：方法路径?参数名=参数值&参数名=参数值
    @RequestMapping(value = "/testParam",
            method = RequestMethod.GET)
    public String testParam(@RequestParam("name") String name, @RequestParam("love") String love) {
        return "鄙人name叫:" + name + " love:" + love;
    }

    //    二、路径传参
//     浏览器网址： http://localhost:8866/param/testPath/haha/123
//    路径传参的语法格式：方法路径/{参数名}/{参数名}
//    路径传参一般会配合 @PathVariable 注解使用去绑定参数
    @RequestMapping(value = "/testPath/{name}/{age}", method = RequestMethod.GET)
    public String testPath(@PathVariable("name") String name,
                           @PathVariable(value = "age") String age) {
        System.out.println("name:" + name + " age:" + age);
        return "name:" + name + " age:" + age;
    }


//    三、接收json格式数据
//   浏览器提交json数据：http://localhost:8866/param/testJson
//    请求体数据格式：｛
//        "name":"tom",
    //    "age": 3
//        "kinds": "金渐层"
    //    ｝
//    配合 @RequestBody 注解使用，将接收json数据格式转换为对象


    @RequestMapping(value = "/testJson", method = RequestMethod.POST)
    public String getCat(@RequestBody Cat cat) {
        return cat.toString();
    }


//    四、接受cookie数据  用@CookieValue注解来绑定方法中的形参与路径参数
//    1、保存cookie

    //    路径 为：http://localhost:8866/param/saveCookie
    @RequestMapping(value = "/saveCookie", method = RequestMethod.GET)
    public String saveCookie(HttpServletResponse response) {
        response.addCookie(new Cookie("name", "zhangsan"));
        return "保存cookie成功";
    }
//    2、获取cookie

    @RequestMapping(value = "/getCookie", method = RequestMethod.GET)
    public String getCookie(@CookieValue("name") String name) {
        return "获取的cookie为:" + name;
    }


//    五、接受请求头header数据  用@RequestHeader注解来绑定方法中的形参与路径参数
//    需要注意：请求头中的参数名是不区分大小写的，但是需要仔细核对请求头是否写错了

//    localhost:8866/param/testHeader

    @RequestMapping(value = "/testHeader", method = RequestMethod.GET)
    public String testHeader(@RequestHeader("User-Agent") String userAgent,
                             @RequestHeader("Host") String host) {
        return "获取的请求头为:" + userAgent + " 主机地址为:" + host;
    }


//    六、原生Api的使用
    //    1、获取请求头数据
//    localhost:8866/param/testOriginalApi
    @RequestMapping(value = "/testOriginalApi",method= RequestMethod.GET)
    public String testOriginalApi(HttpSession session, HttpServletRequest request){
        Enumeration<String> headers = request. getHeaderNames();
        while (headers.hasMoreElements()){
            String headerName = headers.nextElement();
            String headerValue = request.getHeader(headerName);
            System.out.println(headerName+":"+headerValue);
        }



        return "获取的sessionId为:" + session.getId();

    }


}
